package com.smart.city.logger;

//import java.util.List;

import android.app.Activity;
import android.content.Context;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;


public class LocationSensor extends Activity implements Sensable  {

	private Float[] value;
	private LocationManager sensorManager = null;
//	private Location locationSensor = null;
	private LocationSensor instance;
	
	
	public TextView tvLatitude;
	public TextView tvLongitude;
	
	public LocationSensor(Context context) {
		this.instance = this;
		this.value = new Float[] {0f, 0f};
		this.sensorManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
		//LocationListener locationlistener = new myLocationListener();
	    //this.sensorManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 1, locationlistener);
	    
		//tvLatitude = (TextView)findViewById(com.smart.city.R.id.tvLatitude);
	    //tvLongitude = (TextView)findViewById(com.smart.city.R.id.tvLongitude);
	    
	    this.sensorManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 3000, 0, new LocationListener() {
	    	public void onProviderDisabled(String provider) {}
			public void onProviderEnabled(String provider) {}
			public void onStatusChanged(String provider, int status,
					Bundle extras) {}
			
			public void onLocationChanged(Location location) {
				synchronized (instance) {
					instance.value = new Float[2];
					instance.value[0] = (float) location.getLatitude();
					instance.value[1] = (float) location.getLongitude();
					
					//tvLatitude.setText(Double.toString(location.getLatitude()));
			        //tvLongitude.setText(Double.toString(location.getLongitude()));
				}
			}
	    });
	}
	    
			
	public String getFormatedString() {
		Float[] tempSensorValue = getCurrentValue();
		String returnString = String.format("/%.6f/%.6f/",
				tempSensorValue[0], tempSensorValue[1]);
		return returnString;
	}
	
	public Float[] getValue() {
		return getCurrentValue();
	}
	
	synchronized private Float[] getCurrentValue() {
		return this.value.clone();
	}
}


